limsSampleStaging.editPage = {

    getGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: 'lims-sample-staging-edit-list-grid',
            url: limsSampleStaging.baseUrl + '/queries',
            service: 'limsSampleStagingServiceImpl',
            dbTable: 'T_LIMS_SAMPLE_STAGING',
            columns: limsSampleStaging.getEditListColumns(),
            page: true,
            requestData: {
                status_DIFFER: 'done'
            },
            deleteFormatter: function (row) {
                return row.stagingCode
            },
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    Gikam.getAsyncComp('lims-sample-staging-edit-uploader').done(function (comp) {
                        comp.setOptions({
                            bizId: -1,
                        })
                        comp.setToolbarVisible(false)
                    })
                }
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp('lims-sample-staging-edit-uploader').done(function (comp) {
                    comp.setOptions({
                        bizId: row.id,
                    })
                    comp.setToolbarVisible(true)
                })
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.SAMPLEMANAGE.SAMPLESTAGINGS.MODAL.ADD',
                        fields: limsSampleStaging.getBaseInfoFormFields(),
                        url: limsSampleStaging.baseUrl,
                        disableNotEmptyValidate: true
                    }).done(function (id) {
                        Gikam.getComp('lims-sample-staging-edit-list-grid').refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp('lims-sample-staging-edit-list-grid').deleteRows(limsSampleStaging.baseUrl)
                }
            }, {
                type: 'button',
                text: 'CORE.MODULE.ITEM.CUSTOM_QUERIES.BUTTON.COMPLETE',
                icon: 'submit', color: 'success',
                onClick: function () {
                    _this.solve('lims-sample-staging-edit-list-grid', 'done')
                }
            }]
        }
    },

    solve: function (gridId, status) {
        var _this = this
        var grid = Gikam.getComp(gridId)
        var selections = grid.getSelections()
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM')
            return
        }
        Gikam.confirm('GIKAM.MODAL.SUBMIT', 'GIKAM.TIP.SUBMIT', function () {
            let url = limsSampleStaging.baseUrl + '/change-status'
            Gikam.postText(url, Gikam.getJsonWrapper({status: status}, [null, selections])).done(req => {
                req = JSON.parse(req)
                grid.refresh()
            })
        })
    },

    getDoneGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: 'lims-sample-staging-edit-list-grid-done',
            service: 'limsSampleStagingServiceImpl',
            dbTable: 'T_LIMS_SAMPLE_STAGING',
            columns: limsSampleStaging.getShowListColumns(),
            page: true,
            requestData: {
                status_EQ: 'done'
            },
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    Gikam.getAsyncComp('lims-sample-staging-edit-uploader').done(function (comp) {
                        comp.setOptions({
                            bizId: -1,
                        })
                        comp.setToolbarVisible(false)
                    })
                }
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp('lims-sample-staging-edit-uploader').done(function (comp) {
                    comp.setOptions({
                        bizId: row.id,
                    })
                    comp.setToolbarVisible(false)
                })
            },
            toolbar: []
        }
    },

    getMainTab: function () {
        var _this = this
        var first = false
        return {
            type: 'tab',
            id: 'main-tab',
            panels: [{
                title: 'BASEMODULE.SAMPLEMANAGE.SAMPLESTAGINGS.TAB.UN_SOLVE',
                items: [_this.getGridParam()]
            }, {
                title: 'BASEMODULE.SAMPLEMANAGE.SAMPLESTAGINGS.TAB.SOLVED',
                items: [_this.getDoneGridParam()]
            }],
            onActived: function (data) {
                switch (data.index) {
                    case 0:
                        if (first) {  // 首次加载页面时，不刷新，避免进来就刷新两次表格
                            Gikam.getComp('lims-sample-staging-edit-list-grid').refresh({
                                url: limsSampleStaging.baseUrl + '/queries',
                            })
                        } else {
                            first = true
                        }
                        break
                    case 1:
                        Gikam.getComp('lims-sample-staging-edit-list-grid-done').refresh({
                            url: limsSampleStaging.baseUrl + '/queries',
                        })
                        break
                }
            }
        }
    },

    getBaseInfoTab: function () {
        var _this = this
        return {
            type: 'tab',
            panels: [{
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    id: 'lims-sample-staging-edit-uploader',
                    dbTable: 'T_LIMS_SAMPLE_STAGING',
                }]
            }]
        }
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-sample-staging-edit-list-layout',
            renderTo: workspace.window.$dom,
            north: {
                height: '60%',
                items: [this.getMainTab()]
            },
            center: {
                items: [this.getBaseInfoTab()]
            }
        })
    },

    init: function () {
        this.create()
    }
}
